<template>
	<gui-page ref="guiPage" :customHeader="true">
		<template v-slot:gHeader>
			<nav-bar-custom left-icon="left" :title="$t('distributedOut.pageTitle')"></nav-bar-custom>
		</template>
		<template v-slot:gBody>
			<view class="distributed">
				<view class="gui-bg-white gui-dark-bg-level-3 gui-padding-x distributed-head">
					<view class="gui-form-item gui-border-b">
						<text class="gui-form-label gui-secondary-text"
							style="width:300rpx">{{ $t('pageTranslation.noticeOfDelivery') }}</text>
						<view class="gui-form-body gui-text-right">
							<input type="text" class="gui-form-input gui-text-right" v-model="data.oddNum"
								:placeholder="$t('distributedEnter.placeholderText')" />
						</view>
					</view>
					<view class="gui-form-item gui-border-b">
						<text class="gui-form-label gui-secondary-text"
							style="width:300rpx">{{ $t('translationImage.documentationDate') }}</text>
						<view class="gui-form-body gui-text-right" @click="dateChoose">
							<view class="gui-form-input gui-text-right" v-if="data.chooseTime.length">
								<text>{{ data.chooseTime[0] }}</text><text v-if="data.chooseTime[1]"> -
									{{ data.chooseTime[1] }}</text>
							</view>
							<input v-else type="button" class="gui-form-input gui-text-right"
								:placeholder="$t('distributedEnter.placeholderTextChoose')" />
						</view>
					</view>
					<view class="gui-form-item gui-border-b">
						<text class="gui-form-label gui-secondary-text"
							style="width:300rpx">{{ $t('pageTranslation.statusOfDelivery') }}</text>
						<view class="gui-form-body gui-text-right" style="overflow: visible;">
							<superweiCombox style="text-align: right;" :candidates="data.selectList" :defaultValue="$t('smashRemoval.all')"
								:placeholder="$t('stockManage.placeholder')" v-model="data.selectText" :isJSON="true"
								keyName="name" @select="select"></superweiCombox>
						</view>
					</view>
					<view class="gui-form-item gui-border-b gui-flex gui-justify-content-center">
						<view class="gui-flex gui-space-between gui-align-items-center"
							style="width: 70%;height: 100rpx;">
							<button type="default" class="gui-button gui-button-mini gui-bg-black4 gui-noborder"
								style="width:200rpx;" @click="reset()">
								<text
									class="gui-block gui-color-black gui-button-text-mini gui-icons">{{ $t('distributedEnter.reset')
									}}</text>
							</button>
							<button type="default" class="gui-button gui-button-mini gui-bg-blue gui-noborder"
								style="width:200rpx;" @click="handelSearch(OPERATION_SEARCH)">
								<text
									class="gui-block gui-color-white gui-button-text-mini gui-icons">{{ $t('distributedEnter.search')
									}}</text>
							</button>
						</view>
					</view>
				</view>
				<!-- 底部显示区域 -->
				<scroll-view :show-scrollbar="false" :scroll-y="true" class="scroll" @scrolltolower="scrolltolowerFn">
					<view style="height: 15rpx"></view>
					<view class="card-boxCustom gui-transition-all" v-for="(x, i) in data.list" @click="goPage(x)"
						:key="i">
						<view class="card-head">
							<text>{{ $t('pageTranslation.noticeOfDelivery') }}：{{ x.dnCode }}</text>
							<uni-tag v-if="x.waitDeliveryQty > 0" :inverted="true" :text="$t('smashRemoval.unshipped')"
								type="success" />
							<uni-tag v-else-if="x.waitDeliveryQty == 0" :inverted="true"
								:text="$t('smashRemoval.delivered')" type="primary" />
						</view>
						<view class="card-body" style="padding-bottom: 15rpx;">
							<view class="card-rotating" @click.stop="data.listShow[i] = !data.listShow[i]">
								<uni-icons v-if="!data.listShow[i]" type="bottom" color="#9C9C9C" size="20"></uni-icons>
								<uni-icons v-if="data.listShow[i]" type="top" color="#9C9C9C" size="20"></uni-icons>
							</view>
							<view class="card-body-item">
								<text class="body-item-title">{{ $t('translationImage.documentationDate')
									}}：</text>{{ x.establishTime }}
							</view>
							<view class="card-body-item">
								<text class="body-item-title">{{ $t('distributedEnter.compiler') }}：</text>
								{{ x.createUserName }}
							</view>
							<view class="card-body-item">
								<text class="body-item-title">{{ $t('pageTranslation.totalReceipt') }}：</text>
								{{ x.sumQty }}
							</view>
							<view class="card-body-item">
								<text class="body-item-title">{{ $t('newTranslation.outTotal') }}：</text>
								{{ (Number(x.sumQty) - Number(x.scanQty)).toFixed(2) }}
							</view>
						</view>
						<view class="card-body-folding" v-show="data.listShow[i]" v-if="x.wmsDnDs.length > 0">
							<view class="gui-border-t" style="padding: 20rpx 20rpx 0 20rpx;"
								v-for="(k, index) in x.wmsDnDs" :key="index">
								<view class="card-body-item">
									<text class="body-item-title">{{ $t('distributedEnter.materialCode') }}：</text>
									{{ k.materialCode }}
								</view>
								<view class="card-body-item">
									<text class="body-item-title">{{ $t('distributedEnter.materialName') }}：</text>
									{{ k.materialName }}
								</view>
								<view class="card-body-item">
									<text class="body-item-title">{{ $t('distributedEnter.materialSpec') }}：</text>
									{{ k.spec }}
								</view>
								<view class="card-body-item">
									<text class="body-item-title">{{ $t('stockManage.materialUnit') }}：</text>
									{{ k.saleUnit }}
								</view>
								<view class="card-body-item">
									<text class="body-item-title">{{ $t('distributedEnter.materialQty') }}：</text>
									{{ k.saleQty }}
								</view>
								<view class="card-body-item">
									<text class="body-item-title">{{ $t('materials.totalNumberOutgoing') }}：</text>
									{{ k.scanQty }}
								</view>
							</view>
						</view>
					</view>
					<view v-if="data.isFinish" class="noticeHooks">
						{{ data.list.length == 0 ? $t('stockManage.notAvailable') : $t('stockManage.without') }}
					</view>
				</scroll-view>
			</view>
			<calendarCustom ref="calendarCustomRef" @setTime="getTime" :selectTime="data.chooseTime"></calendarCustom>
		</template>
	</gui-page>
</template>
<script setup>
import searchCustom from '@/components/searchCustom';
import { onShow } from '@dcloudio/uni-app';
import calendarCustom from '@/components/calendarCustom';
import superweiCombox from '@/components/superweiCombox';
import { ref, reactive, onMounted, getCurrentInstance } from 'vue';
import graceJS from "@/Grace6/js/grace.js";
import { navigate } from '@/utils/routerSkip.js';
import { basPdaOperationLogFn } from '@/utils/index.js';
import { OPERATION_SEARCH, OPERATION_REASCT, OPERATION_LOOK, OPERATION_CHOOSE } from '@/constant/index.js'
import { findk3DnHs } from '@/api/api.js'
import { useStore } from "vuex";
//JS国际化引入
import { useI18n } from 'vue-i18n';

const { t } = useI18n();
components: {
	superweiCombox,
		searchCustom,
		calendarCustom
}
let store = useStore();
let calendarCustomRef = ref(null);

let data = reactive({
	selectList: [ {
		id: '',
		name: t('smashRemoval.all')
	},{
		id: 'A',
		name: t('smashRemoval.unshipped')
	}, {
		id: 'B',
		name: t('smashRemoval.delivered')
	}],
	chooseTime: [],
	oddNum: '',
	selectId: '',
	selectText: t('smashRemoval.all'),
	list: [],
	listShow: [],
	page: 1,
	rows: 10,
	isFinish: false,
	isLoading: false,
	operationType: '', // 操作日志操作类型
	noticeHooks: false, //暂无相关数据标识
});
const { ctx } = getCurrentInstance();
onMounted(() => {
	getSystemTimeRequest()
	selectDefault()

})
onShow(() => {
	// // await getSystemTimeRequest()
	// search(OPERATION_LOOK)
	getSystemTimeRequest()
})
const getSystemTimeRequest = () => {
	store.dispatch('getSystemTimeRequest')
		.then(res => {
			console.log(res, '----param-----')
			if (res.length) {
				let a = worksTime(res)
				data.chooseTime.push(a)
				data.chooseTime.push(res)
				search(OPERATION_LOOK)
			}
		})
}

const worksTime = (a) => {
	var start = new Date(a).getTime() / 1000
	var end = start - (60 * 60 * 24 * 7)
	var lastDate = new Date(parseInt(end) * 1000).getFullYear() + "-" + (new Date(parseInt(end) * 1000).getMonth() + 1) + "-" + new Date(parseInt(end) * 1000).getDate()
	return lastDate
}
const selectDefault = () => {
	data.selectList.forEach(x => {
		if (!x.id) {
			data.selectText = x.name
		}
	})
}
const select = (e) => {
	console.log(e.id);
	console.log(e.name);
	data.selectId = e.id;
	data.selectText = e.name;
}
const goPage = (x) => {
	if (x.waitDeliveryQty == 0) return uni.showToast({ title: t('smashRemoval.delivered'), icon: 'none' })
	try {
		let inputParameter = x.dnCode
		let returnParameter = '无'
		let operationPicture = '分布式调出'
		let callInterface = '无'
		let opertionParmes = {
			inputParameter,
			returnParameter,
			operationPicture,
			callInterface,
			responseCode: '无',//返回状态
			errorMessage: '无',//错误信息
			isPass: 'Y',//是否成功 Y N
			operationType: OPERATION_CHOOSE,
			module: 'WMS',
			source: "PDA",
		}
		basPdaOperationLogFn(opertionParmes)
	} catch (error) {

	}
	navigate({
		url: `/pages/distributed/eletileDistributedOut?id=${x.id}`
	})
}
const handelSearch = (type) => {
	data.page = 1;
	search(type)
}
const search = (type) => {
	data.operationType = type
	let start = ''
	let end = ''
	if (data.chooseTime.length > 0) {
		start = data.chooseTime[0] + ' 00:00:00'
		end = data.chooseTime[1] ? data.chooseTime[1] + ' 23:59:59' : (data.chooseTime[0] || '') + ' 23:59:59'
	}
	let parmes = {
		startTime: start,
		endTime: end,
		status: data.selectId,
		dnCode: data.oddNum,
		current: data.page,
		size: data.rows,
		id: 0,
		Client:'AF'
	}
	findk3DnHs(parmes).then(res => {
		try {
			let inputParameter = JSON.stringify(parmes)
			let returnParameter = JSON.stringify(res)
			let operationPicture = '分布式调出'
			let callInterface = res.rquestUrl
			let opertionParmes = {
				inputParameter,
				returnParameter,
				operationPicture,
				callInterface,
				responseCode: res.code,//返回状态
				errorMessage: res.msg,//错误信息
				isPass: res.code == 0 ? 'Y' : 'N',//是否成功 Y N
				operationType: type,
				module: 'WMS',
				source: "PDA",
			}
			basPdaOperationLogFn(opertionParmes)
		} catch (error) {
		}
		data.isLoading = false;
		if (res.code == 200) {
			var   records  = res.result.items;
			
			data.noticeHooks = false
			records.forEach(x => {
				records.listShow = false
			})
			if (data.page == 1) {
				data.list = records
				if (records.length == 0) {
					data.noticeHooks = true
				}
			} else {
				data.list.push(...records)
			}
			data.noticeHooks = data.list.length >= res.result.total;
			data.isFinish = data.list.length >= res.result.total;
		} else {
			uni.showToast({ title: res.msg, icon: "none" });
		}
	})
}
// 下拉加载
const scrolltolowerFn = () => {
	console.log(data.isFinish, 'data.isFinishdata.isFinish')
	if (!data.isFinish && !data.isLoading) {
		data.page++
		data.isLoading = true;
		search(data.operationType)
	}
}
const reset = () => {
	data.chooseTime = []
	data.oddNum = ''
	data.page = 1;
	search(OPERATION_REASCT)
}
const dateChoose = () => {
	calendarCustomRef.value.open();
}
const getTime = (times) => {
	data.chooseTime = times;
}
</script>
<style lang="scss" scoped>
.body-item-title {
	// width: 140rpx!important;
}

.distributed-head {
	border-bottom-left-radius: 16rpx;
	border-bottom-right-radius: 16rpx;
}

.scroll {
	flex: 1;
	height: 2rpx;
}

.distributed {
	display: flex;
	flex-direction: column;
	height: calc(100vh - 88rpx);
	// height: 100vh;
}

.card-box {
	margin: 0 15rpx;
	box-shadow: 0rpx 0rpx 4rpx 0 #A5A5A5;
	background: #fff;
	padding: 30rpx;
	border-radius: 16rpx;
}

.card-item {
	line-height: 60rpx;
}

.card-bottom {
	margin-bottom: 30rpx;
}

.filtrate-item {
	display: flex;
	align-items: center;
	font-size: 32rpx;
	margin-bottom: 20rpx;

	.filtrate-itemLeft {
		height: 32rpx;
		line-height: 32rpx;
		width: 100rpx;
	}

	.filtrate-itemRight {
		border: 2rpx solid #A5A5A5;
		border-radius: 8rpx;
		overflow: hidden;
		line-height: 40rpx;
		width: 300rpx;
	}
}
</style>
